package be.kuleuven.peno3.mobiletoledo.view.course;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.Data.LocalDAO;
import be.kuleuven.peno3.mobiletoledo.Data.Client.CourseClient;
import be.kuleuven.peno3.mobiletoledo.model.Course;


public class CourseNavigationView extends Activity {
	
	private Course[] courses;
	private LinearLayout ll;

	/**
	 * Initialize the activity, add menuitems to list.
	 */
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		LocalDAO localDao=new LocalDAO(this); 
		localDao.open(); 
		String study_programme=localDao.fetchProperty("studyprogramme");//gets study_programme from the local database.
		localDao.close();
		courses=CourseClient.getCourses(study_programme);
		
		if(courses == null) {
			Toast.makeText(this, "Er is geen internetverbinding", Toast.LENGTH_SHORT).show();
			finish();
		}
		else if(courses.length == 0){
			Toast.makeText(this, "Er zijn geen vakken om weer te geven", Toast.LENGTH_SHORT).show();
			finish();
		}
		else{ 
			String[] menuItems=getCourseNames();
			displayMenuItems(menuItems);
		}
	}
	
	public void whenClicked(View view) {
		TextView v=(TextView) view;
		onClickAnimation(v);
		for (Course course: courses){	//for each loop that links the text that is returned to the appropriate activity.
			if (v.getText().equals(course.getCourseName())){
				Intent i=new Intent(this, CourseView.class);
				i.putExtra("id",course.getCourseID());	//the courseID is added as extra argument.
				i.putExtra("name",course.getCourseName());
				activityStartDelay(i);
			}
		}
	}
	
	private String[] getCourseNames(){
		if (courses!=null){
			String[] courseNames=new String[courses.length];
			int i=0;
			while (i<courses.length){
				courseNames[i]=courses[i].getCourseName();
				i++;
			}
			return courseNames;
		}
		else {
			String [] courseNames=null;
			return courseNames;
		}
		
	}
	
	private void displayMenuItems (String[] menuItems){
		setContentView(R.layout.textlistlayout);
		TextView courseBar = (TextView) findViewById(R.id.top_bar);
		courseBar.setText("Vakken");
		
		ll = (LinearLayout) findViewById(R.id.man_quest_ll);
				
		for (int i = 0; i < menuItems.length; i++){
			if(i > 0){
				ImageView image = new ImageView(this.getApplicationContext());
				image.setImageDrawable(getResources().getDrawable(R.drawable.line));
				ll.addView(image);
			}
					
			TextView text = new TextView(this.getApplicationContext());
			text.setText(menuItems[i]);
			text.setClickable(true);
			text.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10);
			text.setGravity(Gravity.CENTER_HORIZONTAL);
			text.setOnClickListener(new View.OnClickListener() {
						
				@Override
				public void onClick(View v) {
					whenClicked(v);
				}
			}
			);
			ll.addView(text);
		}
	}
	private void onClickAnimation(View view){
		view.startAnimation(AnimationUtils.loadAnimation(this, R.layout.image_click));
	}
	
	private void activityStartDelay(Intent intent){
		int delay = 400; //Time to delay activity startup
		final Intent i = intent;
		final Handler handler = new Handler(); 
        Timer t = new Timer(); 
        t.schedule(new TimerTask() { 
        	public void run() { 
        		handler.post(new Runnable() { 
        			public void run() { 
        				startActivity(i); 
        			} 
        		}); 
        	} 
        }, delay);
	}

}
